﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cms.Web.CmsSystem.SysModules
{
    public partial class tabpage : System.Web.UI.Page
    {
        Cms.BLL.TabPage bll = new Cms.BLL.TabPage();
        protected void Page_Load(object sender, EventArgs e)
        {           
            if(!IsPostBack )
            BindTreeNode();
        }

        private void BindTreeNode()
        {
           /// Cms.BLL.TabPage bll = new Cms.BLL.TabPage();
            List<Model.Sys_Tabpage> list = bll.GetAll();
            //TreeNode tn = new TreeNode();
            List<Model.Sys_Tabpage> roots = GetRoot(list);
            if (roots.Count >0)
            {
                tvTabPage.Nodes.Clear();
                if (roots.Count == 1)
                    tvTabPage.Nodes.Add(BuildTreeNode(list, roots[0]));
                else {
                    //TreeNode rootNode = new TreeNode();
                    //rootNode.Text = "根节点";
                    //rootNode.Value = "";
                    //foreach()
                    foreach (Model.Sys_Tabpage model in roots) {
                        tvTabPage.Nodes.Add(BuildTreeNode(list, model));
                    }
                }
                tvTabPage.ExpandAll();
                //tvTabPage.ExpandAll();
                
            }

        }

        public TreeNode BuildTreeNode(List<Model.Sys_Tabpage> list, Model.Sys_Tabpage model)
        {
            TreeNode result = new TreeNode();
            if (model != null)
            {
                result.Text = model.Title;
                result.Value = model.Id;
                //result.Expand();
            }
            List<Model.Sys_Tabpage> childs = GetChilds(list, model.Id);
            if (childs.Count > 0) {
                foreach (Model.Sys_Tabpage ml in childs)
                {
                    result.ChildNodes.Add(BuildTreeNode(list, ml));
                }
            }
            return result;
        }

        public List<Model.Sys_Tabpage> GetRoot(List<Model.Sys_Tabpage> list)
        {
            List<Model.Sys_Tabpage> result = new List<Cms.Model.Sys_Tabpage>();
            foreach (Model.Sys_Tabpage model in list)
            {
                if (String.IsNullOrEmpty(model.Parentid))
                {
                    //return model;
                    result.Add(model);
                }
            }
            return result;
        }
        public List<Model.Sys_Tabpage> GetChilds(List<Model.Sys_Tabpage> list, string parentID)
        {
            List<Model.Sys_Tabpage> result = new List<Cms.Model.Sys_Tabpage>();
            foreach (Model.Sys_Tabpage model in list)
            {
                if (String.IsNullOrEmpty(model.Parentid) && String.IsNullOrEmpty(parentID))
                {
                    result.Add(model);
                }
                else if (!String.IsNullOrEmpty(model.Parentid))
                    if (model.Parentid.Equals(parentID, StringComparison.CurrentCultureIgnoreCase))
                        result.Add(model);
            }
            return result;
        }

        private void SetButtonStatus(bool status) {
            this.btnAddChild.Enabled = status;
            this.btnAddRoot.Enabled = status;
            this.btnDelete.Enabled = status;
            this.btnMoveChild.Enabled = status;
            this.btnMoveNext.Enabled = status;
            this.btnMoveParent.Enabled = status;
            this.btnMovePre.Enabled = status;
            this.btnSave.Enabled = status;
        }

        private void SetControlStatus(bool status) {
            this.txtAlias.Enabled = status;
            this.txtIsSys.Enabled = status;
            this.txtLayOut.Enabled = status;
            this.txtTitle.Enabled = status;
        }

        private void ClearText() {
            this.txtAlias.Text = "";
            this.txtIsSys.Checked = false;
            this.txtLayOut.Text = "";
            this.txtTitle.Text = "";
            this.HDID.Value = "";
            this.HDParentID.Value  = "";
        }

        protected void btnAddRoot_Click(object sender, EventArgs e)
        {
            SetControlStatus(true);
            SetButtonStatus(false);
            this.btnSave.Enabled = true;
            this.HDID.Value = System.Guid.NewGuid().ToString();
            this.btnSave.CommandArgument = "AddRoot";
            ClearText();
            this.HDParentID.Value = "";
        }

        private Model.Sys_Tabpage GetModelFromUI()
        {
            string Parentid = this.HDParentID.Value;
            string Id = this.HDID.Value;
            string Title = this.txtTitle.Text;
            string Alias = this.txtAlias.Text;
            bool Is_System = this.txtIsSys.Checked;
            string Layout = this.txtLayOut.Text;

            Model.Sys_Tabpage model = new Cms.Model.Sys_Tabpage();
            model.Parentid = Parentid;
            model.Id = Id;
            model.Title = this.txtTitle.Text;
            model.Alias = this.txtAlias.Text;
            model.Is_System = this.txtIsSys.Checked;
            model.Layout = Layout;
            model.Tab_Type = this.ddlTabType.SelectedValue;
            return model;

        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            //Cms.BLL.TabPage bll = new Cms.BLL.TabPage();
            Model.Sys_Tabpage model = GetModelFromUI();
            bll.SaveOrUpdate(model);
            bll.CommitChanges();
            BindTreeNode();
            SetButtonStatus(true);
            this.btnSave.Enabled = false;
        }

        protected void btnAddChild_Click(object sender, EventArgs e)
        {
            string id = this.HDID.Value;
            SetButtonStatus(false);
            this.btnSave.Enabled = true;
            ClearText();
            this.HDID.Value  = System.Guid.NewGuid().ToString();
            this.HDParentID.Value = id;
        }
        private void ShowInfo(Model.Sys_Tabpage model) {
            txtAlias.Text = model.Alias;
            txtIsSys.Checked = model.Is_System!=null && model.Is_System.Value;
            txtLayOut.Text = model.Layout;
            HDID.Value = model.Id;
            HDParentID.Value = model.Parentid;
            txtTitle.Text = model.Title;
            ddlTabType.SelectedValue = model.Tab_Type;
        }

        protected void tvTabPage_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (tvTabPage.SelectedNode != null)
            {
                string id = tvTabPage.SelectedNode.Value;
               Model.Sys_Tabpage model= bll.GetById(id, false);
               ShowInfo(model);
               SetButtonStatus(true);
            }
        }

        protected void ddlTabType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlTabType.SelectedValue == "1") {
                this.txtLayOut.Text = "/CmsSystem/SysModules/"+HDID.Value +".js";
            }
        }
    }
}
